home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 547 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.1 KB  |  90 lines

  1. Path: chronicle.mti.sgi.com!austern
  2. From: clamage@Eng.Sun.COM (Steve Clamage)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Calling X(int) from X()'s init list
  5. Date: 26 Feb 1996 09:39:12 PST
  6. Organization: Sun Microsystems Inc.
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <4gsmnm$3aq@engnews1.Eng.Sun.COM>
  9. References: <4gsa2r$gjm@sdaw04.seinf.abb.se>
  10. Reply-To: clamage@Eng.Sun.COM
  11. NNTP-Posting-Host: isolde.mti.sgi.com
  12. X-Original-Date: 26 Feb 1996 16:22:14 GMT
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBVAwUBMTHwTEy4NqrwXLNJAQE0uwH+OTFQPxcyrHW6Wa4trbUeilR7RNWYPbzq
  15.     MQ3WSf1qGAktWee1eG7ONyjOwG5ptUEmL7Pxj3ARTUqyrgw9ZqKEjg==
  16.     =uA1j
  17. Originator: austern@isolde.mti.sgi.com
  18.  
  19. In article gjm@sdaw04.seinf.abb.se, alindbac@sw.seisy.abb.se (Anders Lindback) writes:
  20. >In article <4gognf$et@news.bridge.net>,
  21. >David Byrden  <100101.2547@compuserve.com> wrote:
  22. >><<<<<<<
  23.  
  24. >>Why is it not allowed to delegate the initialisation to another
  25. >>constructor
  26.  
  27. >>class X
  28. >>{
  29. >>public:
  30. >>    X (int i) : i_(i) {}
  31. >>
  32. >>    X () : X(34) {}             // Not allowed
  33. >>>>>>>>>>>
  34. >>
  35. >>     Why not use      X (int i = 34 ) : i_(i) {}
  36.  
  37. >>[ I think he was concerned with the more general case of a complicated
  38. >>  initialization (not assignment) that must be repeated for each
  39. >>  constructor. -sdc, moderator
  40. >>]
  41.  
  42. >Well, one could use a similar thing using a member function:
  43. >
  44. >X (int i) { init(i); }
  45. >X () { init(34); }
  46. >
  47. >where init is a member function for class X.
  48.  
  49. That does not solve the *initialization* problem. Consider this example:
  50.  
  51. class X {
  52.     const int i;
  53.     ...
  54. public:
  55.     X() : i(0) { ... more stuff }
  56.     X(int k) : i(k) { ... more stuff }
  57.     X(const X& rhs) : i(rhs.i) { ... more stuff }
  58. };
  59.  
  60. Because member 'i' is const, it cannot be assigned to, but must be
  61. initialized. You cannot break out its initialization into a common
  62. subroutine, but must repeat the initialization on every constructor.
  63. In this toy example that is not a hardship, but suppose there were
  64. many items, and each had a more complicated initialization.
  65.  
  66. Similarly, suppose 'i' were a class type that did not have a no-argument
  67. constructor, or a reference. You would have to provide an initializer on
  68. every constructor.
  69.  
  70. Finally, suppose 'i' were a class type that did have a no-argument
  71. constructor, but you did not want the default initialization in
  72. this class. You could let the default initialization occur then fix
  73. up the value in a common subroutine. That approach is often wasteful,
  74. particularly if it involves allocating and freeing memory, opening
  75. and closing files, or setting and clearing locks.
  76.  
  77. I think the original question is really about a way to break out
  78. a member-init-list into a common bit of code somehow, which cannot
  79. currently be done. It seems to me it is (only) a convenience and ease-of-
  80. maintenance issue.
  81. ---
  82. Steve Clamage, stephen.clamage@eng.sun.com
  83. ---
  84. [ To submit articles: Try just posting with your newsreader.  If that fails,
  85.                       use mailto:std-c++@ncar.ucar.edu
  86.   FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  87.   Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  88.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  89. ]
  90.